package com.hzit.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import javafx.util.converter.LocalDateTimeStringConverter;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Configuration
@MapperScan("com.hzit.mapper")
public class MyBatisPlusConfig implements MetaObjectHandler {

	// 配置分页插件
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		// 配置乐观锁插件
		interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
		interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
		//interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
		return interceptor;
	}

	// 插入时填充字段内容
	@Override
	public void insertFill(MetaObject metaObject) {
		this.setFieldValByName("createTime",LocalDateTime.now(),metaObject);
		this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
	}
	// 修改时填充
	@Override
	public void updateFill(MetaObject metaObject) {
		this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
	}


}
